// 
//  Copyright (C) 2009 Jacek Trubłajewicz
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 

using System;
using System.Drawing;
using System.Collections.Generic;
using HAKGERSoft;

namespace wxGadu
{
	
	[Serializable]
	public class UserConfig : ICloneable
	{
		#region gadu settings
		private string uin;
		// moved to AppConfig because System.NonSerializedAttribute() 
		// doesn't work
//		[System.NonSerializedAttribute()]
//		private string password;
		private string name;
		private GGStatusType defaultStatus;
		private bool saveDescription;
		private bool automaticGaduServer;
		private string gaduServerAddress;
		private int connectionTimeout;
		#endregion
		
		#region appearance
		[System.Runtime.Serialization.OptionalFieldAttribute]
		private Colour contactListBackground;
		[System.Runtime.Serialization.OptionalFieldAttribute]
		private Colour infoPanelBackground;
		[System.Runtime.Serialization.OptionalFieldAttribute]
		private Font contactListFont;
		[System.Runtime.Serialization.OptionalFieldAttribute]
		private Font infoPanelFont;
		[System.Runtime.Serialization.OptionalFieldAttribute]
		private Font topChatWindowFont;
		[System.Runtime.Serialization.OptionalFieldAttribute]
		private Font bottomChatWindowFont;
//		[System.Runtime.Serialization.OptionalFieldAttribute]
		private Size chatWindowSize;
//		[System.Runtime.Serialization.OptionalFieldAttribute]
		private Size mainWindowSize;
		#endregion
		
		#region behaviour
//		[System.Runtime.Serialization.OptionalFieldAttribute]
		private Point mainWindowPosition;
//		[System.Runtime.Serialization.OptionalFieldAttribute]
		private Point chatWindowPosition;
		private bool enterSend;
		private bool enableTrayIcon;
		private bool confirmExit;
		private LogLevel fileLoggingLevel;
		private LogLevel consoleLoggingLevel;
		private bool showTooltips;
		private int tooltipsDelay;
//		private Point notifyWindowPosition;
		private bool showNewChatNotify;
		private int notifyTtl;
		private bool closeToTray;
		private int chatWindowMessagesCount;
		private bool saveHistory;
		#endregion
		
		#region contact status tracking
		private bool enableContactStatusNotify;
		private bool showOnlyAvailableNotify;
		private List<int> trackedContacts;
		#endregion
		private bool emptyDesc;
		
		public UserConfig()
		{
			SetDefaultValues();
		}
		
		/// <summary>
		/// Apply default values to all fields, except <c>uin</c>
		/// </summary>
		public void SetDefaultValues()
		{
			if (uin == null)
				uin = string.Empty;
//			password = string.Empty;
			name = System.Environment.UserName;
			defaultStatus = GGStatusType.NotAvailable;
			saveDescription = true;
			automaticGaduServer = true;
			connectionTimeout = 2000;
			contactListBackground = null;
			infoPanelBackground = null;
			contactListFont = null;
			infoPanelFont = null;
			topChatWindowFont = null;
			bottomChatWindowFont = null;
			
			mainWindowSize = new Size(200, 440);
			mainWindowPosition = new Point(0, 0);
			chatWindowSize = new Size(345, 360);
			wx.Display disp = wx.Display.GetDisplay(0);
			chatWindowPosition = new Point(0, disp.Geometry.Height - chatWindowSize.Height < 0 ? 0 : disp.Geometry.Height - chatWindowSize.Height );
			enterSend = true;
			enableTrayIcon = true;
			confirmExit = false;
			fileLoggingLevel = LogLevel.Warn;
			consoleLoggingLevel = LogLevel.Warn;
			showTooltips = true;
			tooltipsDelay = 2000;
			showNewChatNotify = true;
			notifyTtl = 4000;
			closeToTray = true;
			chatWindowMessagesCount = 10;
			saveHistory = true;
			
			enableContactStatusNotify = false;
			showOnlyAvailableNotify = true;
			trackedContacts = new List<int>();
		}
				
		//// <value>
		/// User GG numeber
		/// </value>
		public string Uin
		{
			get { return uin; }
			set { uin = value; }
		}

		//// <value>
		/// User name
		/// </value>
		public string Name
		{
			get { return name; }
			set { name = value; }
		}
		
		//// <value>
		/// Status automaticaly set after application start
		/// </value>
		public GGStatusType DefaultStatus
		{
			get { return defaultStatus; }
			set { defaultStatus = value; }
		}
		
		//// <value>
		/// Save entered descriptions
		/// </value>
		public bool SaveDescription
		{
			get { return saveDescription; }
			set { saveDescription = value; }
		}
		
		//// <value>
		/// Automaticaly choose Gadu server
		/// </value>
		public bool AutomaticGaduServer
		{
			get { return automaticGaduServer; }
			set { automaticGaduServer = value; }
		}
		
		//// <value>
		/// Gadu server address
		/// </value>
		public string GaduServerAddress
		{
			get { return gaduServerAddress; }
			set { gaduServerAddress = value; }
		}
		
		//// <value>
		/// Time to wait for connection to Gadu server
		/// </value>
		/// <remarks>
		/// In miliseconds.
		/// </remarks>
		public int ConnectionTimeout
		{
			get { return connectionTimeout; }
			set { connectionTimeout = value; }
		}
		
		//// <value>
		/// Color of <c>MainFrame.TopWindow.ContactListCtrl</c> 
		/// background
		/// </value>
		public Colour ContactListBackground
		{
			get { return contactListBackground; }
			set { contactListBackground = value; }
		}
		
		//// <value>
		/// Color of <c>MainFrame.BottomWindow.InfoPanel</c> 
		/// background
		/// </value>
		public Colour InfoPanelBackground
		{
			get { return infoPanelBackground; }
			set { infoPanelBackground = value; }
		}
		
		//// <value>
		/// Font used in <c>MainFrame.TopWindow.ContactListCtrl</c> 
		/// </value>
		public Font ContactListFont
		{
			get { return contactListFont; }
			set { contactListFont = value; }
		}
		
		//// <value>
		/// Font used in <c>MainFrame.BottomWindow.InfoPanel</c> 
		/// </value>
		public Font InfoPanelFont
		{
			get { return infoPanelFont; }
			set { infoPanelFont = value; }
		}
		
		//// <value>
		/// Font used in <c>ChatFrame.TopChatFramePanel.messageWindow</c>
		/// </value>
		public Font TopChatWindowFont
		{
			get { return topChatWindowFont; }
			set { topChatWindowFont = value; }
		}
		
		//// <value>
		/// Font used in <c>ChatFrame.BottomChatFramePanel.inputText</c>
		/// </value>
		public Font BottomChatWindowFont
		{
			get { return bottomChatWindowFont; }
			set { bottomChatWindowFont = value; }
		}
		
		//// <value>
		/// Main app default window size
		/// </value>
		public Size MainWindowSize
		{
			get { return mainWindowSize; }
			set { mainWindowSize = value; }
		}
		
		//// <value>
		/// Chat window default size
		/// </value>
		public Size ChatWindowSize
		{
			get { return chatWindowSize; }
			set { chatWindowSize = value; }
		}
		
		//// <value>
		/// Main app default window position
		/// </value>
		public Point MainWindowPosition
		{
			get { return mainWindowPosition; }
			set { mainWindowPosition = value; }
		}
		
		//// <value>
		/// Chat window default position
		/// </value>
		public Point ChatWindowPosition
		{
			get { return chatWindowPosition; }
			set { chatWindowPosition = value; }
		}
		
		//// <value>
		/// Send messages by pressing enter
		/// </value>
		public bool EnterSend
		{
			get { return enterSend; }
			set { enterSend = value; }
		}
		
		//// <value>
		/// Enable tray icon
		/// </value>
		public bool EnableTrayIcon
		{
			get { return enableTrayIcon; }
			set { enableTrayIcon = value; }
		}
		
		//// <value>
		/// Confirm when exiting application
		/// </value>
		public bool ConfirmExit
		{
			get { return confirmExit; }
			set { confirmExit = value; }
		}
			
		//// <value>
		/// Logging level for file
		/// </value>
		public LogLevel FileLoggingLevel
		{
			get { return fileLoggingLevel; }
			set { fileLoggingLevel = value; }
		}
		
		//// <value>
		/// Logging level for console
		/// </value>
		public LogLevel ConsoleLoggingLevel
		{
			get { return consoleLoggingLevel; }
			set { consoleLoggingLevel = value; }
		}
		
		//// <value>
		/// Show tooltips help
		/// </value>
		public bool ShowTooltips
		{
			get { return showTooltips; }
			set { showTooltips = value; }
		}
		
		//// <value>
		/// Delay after which the tooltip appears.
		/// </value>
		/// <remarks>
		/// In miliseconds.
		/// </remarks>
		public int TooltipsDelay
		{
			get { return tooltipsDelay; }
			set { tooltipsDelay = value; }
		}
		
		//// <value>
		/// Enable or disable new chat notify window
		/// </value>
		public bool ShowNewChatNotify
		{
			get { return showNewChatNotify; }
			set { showNewChatNotify = value; }
		}
		
		//// <value>
		/// Delay after which notify window will be closed.
		/// </value>
		/// <remarks>
		/// In miliseconds.
		/// </remarks>
		public int NotifyTtl
		{
			get { return notifyTtl; }
			set 
			{
				if (value < 100 || value > 15000)
					notifyTtl = 4000;
				else	
					notifyTtl = value; 
			}
		}
		
		//// <value>
		/// Hide main frame in tray insted
		/// of closing application
		/// </value>
		public bool CloseToTray
		{
			get { return closeToTray; }
			set { closeToTray = value; }
		}
		
		//// <value>
		/// Number of messages to show in
		/// <c>ChatFrame.TopChatFramePanel.messageWindow</c>
		/// </value>
		public int ChatWindowMessagesCount
		{
			get { return chatWindowMessagesCount; }
			set 
			{ 
				if (value < 5 || value > 40)
					chatWindowMessagesCount = 10;
				else
					chatWindowMessagesCount = value;
			}
		}
		
		//// <value>
		/// Save chat history
		/// </value>
		public bool SaveHistory
		{
			get { return saveHistory; }
			set { saveHistory = value; }
		}
		
		//// <value>
		/// Show notification about contacts 
		/// status change
		/// </value>
		public bool EnableContactStatusNotify
		{
			get { return enableContactStatusNotify; }
			set { enableContactStatusNotify = value; }
		}
		
		//// <value>
		/// Show notifications only when contact
		/// become available/away
		/// </value>
		public bool ShowOnlyAvailableNotify
		{
			get { return showOnlyAvailableNotify; }
			set { showOnlyAvailableNotify = value; }
		}
				
		//// <value>
		/// List of contacts for which notification
		/// about status change should be 
		/// displayed
		/// </value>
		public List<int> TrackedContacts
		{
			get { return trackedContacts; }
			set { trackedContacts = value; }
		}
		
		//// <value>
		/// Indicates that user don't have any description
		/// now. Application will set description form history
		/// at start only if this is <c>true</c>.
		/// </value>
		public bool EmptyDesc
		{
			get { return emptyDesc; }
			set { emptyDesc = value; }
		}
		
		/// <summary>
		/// Deep copy
		/// </summary>
		/// <returns>
		/// A <see cref="System.Object"/>
		/// </returns>
		public object Clone()
		{
			UserConfig clone = new UserConfig();
			
			clone.Uin = this.uin;
			clone.Name = this.name;
			clone.DefaultStatus = this.defaultStatus;
			clone.saveDescription = this.saveDescription;
			clone.automaticGaduServer = this.automaticGaduServer;
			clone.connectionTimeout = connectionTimeout;
			clone.contactListBackground = contactListBackground;
			clone.infoPanelBackground = infoPanelBackground;
			clone.contactListFont = contactListFont;
			clone.infoPanelFont = infoPanelFont;
			clone.topChatWindowFont = topChatWindowFont;
			clone.bottomChatWindowFont = bottomChatWindowFont;
			
			clone.mainWindowSize = new Size(mainWindowSize.Width, mainWindowSize.Height);
			clone.mainWindowPosition = new Point(mainWindowPosition.X, mainWindowPosition.Y);
			clone.chatWindowSize = new Size(chatWindowSize.Width, chatWindowSize.Height);
			clone.chatWindowPosition = new Point(chatWindowPosition.X, chatWindowPosition.Y);
			clone.enterSend = enterSend;
			clone.enableTrayIcon = enableTrayIcon;
			clone.confirmExit = confirmExit;
			clone.fileLoggingLevel = fileLoggingLevel;
			clone.consoleLoggingLevel = consoleLoggingLevel;
			clone.showTooltips = showTooltips;
			clone.tooltipsDelay = tooltipsDelay;
			clone.showNewChatNotify = showNewChatNotify;
			clone.notifyTtl = notifyTtl;
			clone.closeToTray = closeToTray;
			clone.chatWindowMessagesCount = chatWindowMessagesCount;
			clone.saveHistory = saveHistory;
			
			clone.enableContactStatusNotify = enableContactStatusNotify;
			clone.showOnlyAvailableNotify = showOnlyAvailableNotify;
		
			clone.trackedContacts = new List<int>();
			foreach (int contact in trackedContacts)
				clone.trackedContacts.Add(contact);
	
			clone.emptyDesc = this.emptyDesc;
			
			return clone;
		}
	}
}
